package com.hevs.adibilis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;

import com.hevs.adibilis.R.id;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


	
public class SearchActivities extends Activity {

	private ListView listview;
	private String searchString="";
	private EditText editTextSearch;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		
		this.listview = (ListView) findViewById(R.id.listViewSearchActivities);
		this.editTextSearch = (EditText) findViewById(R.id.editTextSearch);
		
		
		
		editTextSearch.addTextChangedListener(new TextWatcher(){
	        public void afterTextChanged(Editable s) {
	        	setSearchString(editTextSearch.getText().toString());
	        	new DownloadDataFromWeb().execute();
	        }
	        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
	        public void onTextChanged(CharSequence s, int start, int before, int count){}
	    }); 
		
		
		
		
		if(Utils.hasInternet(this)){
			new DownloadDataFromWeb().execute();
		} else {
			Toast.makeText(this, R.string.PleaseConnectToInternet, Toast.LENGTH_LONG).show();
		}
		
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search_activities, menu);
		return true;
	}
	
	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
		Log.d("search", searchString);
	}

	private class DownloadDataFromWeb extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {


		// onPostExecute displays the results of the AsyncTask.
		protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

			/**
			 * Updating parsed JSON data into ListView
			 * */
			ListAdapter adapter = new SimpleAdapter(getApplicationContext(), result,
					R.layout.list_item_activity,
					new String[] { JSONFactory.TAG_ID, JSONFactory.TAG_REFERENCE, JSONFactory.TAG_NAME }, new int[] {
				R.id.idActivity, R.id.referenceActivity, R.id.nameActivityTextView });

			listview.setAdapter(adapter);
			listview.setOnItemClickListener(new OnItemClickListener(){ //

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					// getting values from selected ListItem
					String id_activity = ((TextView) view.findViewById(R.id.idActivity)).getText().toString(); //TODO chercher dans le hash
					String name = ((TextView) view.findViewById(R.id.nameActivityTextView)).getText().toString();

					// Starting new intent
					Intent in = new Intent(getApplicationContext(), DetailActivityActivity.class);
					in.putExtra(JSONFactory.TAG_ID, id_activity);
					in.putExtra(JSONFactory.TAG_NAME, name);

					startActivity(in);
				}

			});

		}

		@Override
		protected ArrayList<HashMap<String, String>> doInBackground(String... searchString) {

			try {
				JSONFactory.getActivitiesSearch(getSearchString());
				return JSONFactory.parseActivitiesSearch();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoNodesException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
			
		}
	}

		
}
